﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
using System.Windows.Forms;


namespace GraphsLab5
{
    public partial class Form1:Form
    {
        private void LoadButton_Click(object sender, EventArgs e)
        {
            try
            {
                points = SQLConnection.getPoints();
                colors = SQLConnection.getColors();
                lines = SQLConnection.getLines();
            }
            catch
            {
                MessageBox.Show("Error while connecting to DB");
            }
            
            dataGridView1.Rows.Clear();
            dataGridView1.Rows.Add(points.Count);
            int r = 0;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (r < points.Count)
                {
                    string join = "";
                    foreach (int n in lines[r]) join += n.ToString() + ",";
                    if (join.EndsWith(",")) join = join.Remove(join.Length - 1);

                    row.HeaderCell.Value = (r + 1).ToString();
                    row.Cells[0].Value = points[r].X;
                    row.Cells[1].Value = points[r].Y;
                    row.Cells[2].Value = colors[r].Name;
                    row.Cells[3].Value = join;
                    r++;
                }
            }
            draw();
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {

            Worker.reviewCell(dataGridView1, pictureBox1, e.RowIndex, e.ColumnIndex);            

        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            DrawButton.PerformClick();
            if (!SQLConnection.saveDB(points, colors, lines))
            {
                MessageBox.Show("Can not save to DB!");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (!Worker.reviewData(dataGridView1, pictureBox1)) return;

            toList();
            draw();

        }

        private void draw()
        {
            Graphics paint = pictureBox1.CreateGraphics();
            paint.Clear(Color.White);
            paint.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            if (points.Count > 0)
            {
                for (int i = 0; i < points.Count; i++)
                {
                    SolidBrush brush = new SolidBrush(colors[i]);
                    if (lines[i].Count > 0)
                    {
                        for (int j = 0; j < lines[i].Count; j++)
                        {
                            if (i != lines[i][j] - 1) paint.DrawLine(new Pen(brush, 3), new Point(points[i].X, points[i].Y), new Point(points[lines[i][j] - 1].X, points[lines[i][j] - 1].Y));
                            else paint.DrawEllipse(new Pen(new SolidBrush(Color.Cyan), 3), new Rectangle(new Point(points[i].X - 2, points[i].Y - 2), new Size(20, 20)));
                        }
                    }                    
                    brush.Dispose();
                }
                for (int i = 0; i < points.Count; i++)
                {
                    SolidBrush brush = new SolidBrush(colors[i]);
                    paint.FillEllipse(brush, new Rectangle(new Point(points[i].X - 5, points[i].Y - 5), new Size(10, 10)));
                    brush.Dispose();
                }
            }
            isDrawn = true;
        }

        private void toList()
        {

            points.Clear();
            colors.Clear();
            lines.Clear();
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (!row.IsNewRow)
                {
                    points.Add(new Point(Int32.Parse(row.Cells[0].Value.ToString()), Int32.Parse(row.Cells[1].Value.ToString())));
                    Color c = new Color();
                    string color = row.Cells[2].Value.ToString();
                    switch (color)
                    {
                        case "Black": { c = Color.Black; break; }
                        case "Red": { c = Color.Red; break; }
                        case "Green": { c = Color.Green; break; }
                        case "Blue": { c = Color.Blue; break; }
                        default: { c = Color.Black; break; }
                    }
                    colors.Add(c);
                    string allLines = "";
                    if (row.Cells[3].Value != null) allLines = row.Cells[3].Value.ToString();
                    List<int> add = new List<int>();
                    if (allLines.Length > 0)
                    {
                        foreach (string s in allLines.Split(','))
                        {
                            if (s != "") add.Add(Int32.Parse(s));
                        }
                    }
                    lines.Add(add);
                }
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Graphics paint = pictureBox1.CreateGraphics();
            paint.Clear(System.Drawing.Color.White);
            dataGridView1.Rows.Clear();
            points.Clear();
            lines.Clear();
            colors.Clear();
            isDrawn = false;
        }

        private void MatrixButton_Click(object sender, EventArgs e)
        {
            Matrix a = Worker.createMatrix(lines);
            MessageBox.Show(a.ToString(), "Connection Matrix", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void ListOfConnections_Click(object sender, EventArgs e)
        {
            ConnectionList a = new ConnectionList(lines);
            MessageBox.Show(a.ToString(), "Connection List", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void LinesList_Click(object sender, EventArgs e)
        {
            ConnectionList a = new ConnectionList(lines);
            MessageBox.Show(a.ToString(true), "List of Lines", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void diametrButton_Click(object sender, EventArgs e)
        {
            ConnectionList a = new ConnectionList(lines);
            List<Point> res;
            double max = a.diametr(points, out res);
            string s = "The maximal distance is equal to " + max.ToString(".###") + " and it's achieved on:\n";
            for (int i = 0; i < res.Count; i++)
            {
                s += (res[i].X + 1).ToString() + " -------> " + (res[i].Y + 1).ToString() + ";\n";
            }
            s = s.Remove(s.Length - 2);
            s += ".";
            MessageBox.Show(s, "Maximal distance", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

    }
}
